﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using LView.CMS.IServices;
using LView.CMS.Models;
using LView.CMS.Core;
using LView.CMS.ViewModels;
using LView.CMS.Core.Extensions;
using System.Linq;

namespace LView.CMS.Services
{
    public class CourseService : ICourseService
    {
        private readonly IAssetsService _assetsService;
        public CourseService(IAssetsService assetsService)
        {
            _assetsService = assetsService;
        }
        public List<LTask> GetTaskList()
        {
            return (MongodbHelper.Instance.FindList<LTask>((x => x.Type != 1), sort: (x => x.Id))).list;
        }

        public List<LFile> GetFileList()
        {
            return (MongodbHelper.Instance.FindList<LFile>(sort: (x => x.Id))).list;
        }

        public List<LExam> GetExamList()
        {
            return (MongodbHelper.Instance.FindList<LExam>(sort: (x => x.Id))).list;
        }

        public async Task<LTask> GetCurrentTask(string taskId)
        {
            return await MongodbHelper.Instance.FindEntityAsync<LTask>(x => x.Id == taskId);
        }

        public async Task<BaseResult> AddCourseAsync(CourseAddOrModify input)
        {
            var result = new BaseResult();
            try
            {
                var taskInfo = MongodbHelper.Instance.FindEntity<LTask>((x => x.Id == input.TaskId));
                var oriCategory = taskInfo.Category;
                MongodbHelper.Instance.DeleteOne<LTask>(x => x.Id == input.TaskId);
                var courseInfo = new Course
                {
                    CourseId = Guid.NewGuid().ToString(),
                    Name = input.CourseName,
                    Description = input.CourseDescription,
                    Type = input.CourseType,
                    CoursePeriod = input.CoursePeriod,
                    CourseScore = input.CourseScore
                };
                if (input.CourseType == "1")
                    courseInfo.ExamId = input.ExamId;
                if (input.CourseType == "0")
                    courseInfo.FileId = input.FileId;

                if (input.CategoryId.IsNullOrEmpty())
                {
                    var category = new Category
                    {
                        CategoryId = Guid.NewGuid().ToString(),
                        Name = input.CategoryName,
                        Description = input.CategoryDescription,
                        CategoryPeriodMin = courseInfo.CoursePeriod,
                        CategoryScore = courseInfo.CourseScore,
                        Course = new List<Course> { courseInfo }
                    };
                    courseInfo.Sort = 1;
                    if (oriCategory == null)
                    {
                        category.Sort = 1;
                        taskInfo.Category = new List<Category> { category };
                    }
                    else
                    {
                        var categorySort = oriCategory.Count + 1;
                        category.Sort = categorySort;
                        oriCategory.Add(category);
                    }
                    await MongodbHelper.Instance.InsertOneAsync<LTask>(taskInfo);
                }
                else
                {
                    var category = taskInfo.Category.FirstOrDefault(x => x.CategoryId == input.CategoryId);
                    var courseSort = category.Course.Count + 1;
                    category.CategoryPeriodMin += courseInfo.CoursePeriod;
                    category.CategoryScore += courseInfo.CourseScore;
                    courseInfo.Sort = courseSort;
                    category.Course.Add(courseInfo);
                    await MongodbHelper.Instance.InsertOneAsync<LTask>(taskInfo);
                }
                result.ResultCode = ResultCodeAddMsgKeys.CommonObjectSuccessCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonObjectSuccessMsg;
            }
            catch (Exception ex)
            {
                result.ResultCode = ResultCodeAddMsgKeys.CommonExceptionCode;
                result.ResultMsg = ResultCodeAddMsgKeys.CommonExceptionMsg + $":{ex.Message}";
            }

            return result;
        }
    }
}
